<!DOCTYPE html>
<html lang="en" xmlns:th="http://thymeleaf.org">
<head>
    <head>
        <th:block th:include="include :: header('职位列表')"/>
    </head>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="searchForm">
                <div class="select-list">
                    <ul>
                        <li>
                            职位名称：<input type="text" id="postName"/>
                        </li>
                        <li>
                            职位编码：<input type="text" id="postCode"/>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" id="searchButton"><i class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" id="resetButton"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" onclick="add();" data-role="system:post:add">
                <i class="fa fa-plus"></i> 新增
            </a>
            <a class="btn btn-primary single disabled" onclick="edit(this);" data-id="0" data-role="system:post:edit">
                <i class="fa fa-edit"></i> 修改
            </a>
            <a class="btn btn-danger multiple disabled" onclick="removeAll(this);" data-id="0" data-role="system:post:delete">
                <i class="fa fa-remove"></i> 删除
            </a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="dataTable"></table>
        </div>
    </div>
</div>

<th:block th:include="include :: footer"/>
<!-- jquery-validate 表单树插件 -->
<script type="text/javascript" th:inline="javascript">
    let $table;
    $(function () {
        loadData();

    });


    function loadData(){
        let options={
            url: '/admin/position/getList',
            pageNumber: 1,                      // 初始化加载第一页，默认第一页,并记录
            pageSize: 10,                       // 每页的记录行数（*）
            pageList: "10, 25, 50, 100",        // 可供选择的每页的行数（*）
            queryParams: function (params) {
                let p = {
                    pageNumber: params.pageNumber,
                    pageSize: params.pageSize,
                    postName:$('#postName').val(),
                    postCode:$('#postCode').val()
                }
                return p;
            },
            columns: [
                {field: 'selectItem', checkbox: true},
                {
                    field: 'postName', title: '职位名称', align: "center"
                },
                {field: 'postCode', title: '职位编码', align: "center"},
                {field: 'postSort', title: '排序', align: "center"},
                {field: 'postStatus', title: '角色状态', align: "center",formatter:function (value,row,index){
                        if(value===1){
                            return '<span class="badge badge-danger">停用</span>'
                        }
                        return '<span class="badge badge-primary">正常</span>';

                    }},
                {field: 'createTime', title: '创建时间', align: "center"},
                {field: 'remark', title: '备注', align: "center"},
                {
                    field: 'id',title: '操作', align: "center", formatter: renderOperationButton
                }
            ]
        };

        $table=$('#dataTable').easyTable(options);

    }

    function  renderOperationButton(value,row,index){
        var actions = [];
        actions.push({
            roleTag:'system:post:edit',
            button:'<a class="btn btn-success btn-xs" href="javascript:void(0);" onclick="edit(this);" data-id="' + value + '"><i class="fa fa-edit"></i>编辑</a>'
        });
        actions.push({
            roleTag:'system:post:delete',
            button:'<a class="btn btn-danger btn-xs" href="javascript:void(0);" onclick="del(this);" data-id="' + value + '"><i class="fa fa-trash"></i>删除</a>'
        });

        return jutils.buttonPermission(actions);
    }


    function add(){
        //jutils.openTab('userTab','添加用户','/admin/sysuser/form?id='+0);
        jutils.dialog('添加职位','/admin/position/form',{id:0},null,function (){
            $table.easyTable("search");
        })
    }

    function edit(obj){
        let id=$(obj).data('id');
        if(id==0){
            const rows=$table.easyTable("selected");
            if(rows.length==0){
                jutils.warn("请选择数据行");
                return;
            }
            id=rows[0].id;
        }
        jutils.dialog('修改职位','/admin/position/form',{id:id},null,function (){
            $table.easyTable("search");
        })
        // jutils.openTab('userTab','修改用户','/admin/sysuser/form?id='+id);
    }

    function removeAll(obj){
        const id=$(obj).data('id');
        let del=false;
        let ids=[];
        if(id==0){
            const rows=$table.easyTable("selected");
            if(rows.length==0){
                jutils.warn("请选择数据行");
                return;
            }
            for (let i=0;i<rows.length;i++){
                ids.push(rows[i].id);
            }
        }else{
            ids.push(id);
        }


        jutils.confirm("确认删职位除数据吗？",function(){
            jutils.ajaxGet("/admin/position/removeAll",{ids:ids.join()},function(res){
                if(res.status){
                    $table.easyTable('search');
                }
            });
        });
    }


</script>
</body>
</html>